const app = getApp()
var cf = require('../../lib/config.js');
var c = require('../../lib/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    who: 1,
    age: 1,
    wstatus: 1,
    experience: 1, //经验
    marriage: 1, //婚姻
    live: 1,
    master: 1, //原居民
    money: 1,
    add: 1, //加班
    attitude: 1, //态度
    topname: '宠物领养小站',
    prepage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var pid = options.pid;
    var that = this;
    that.setData({ pid: pid})
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        var latitude = res.latitude
        var longitude = res.longitude
        that.setData({
          latitude: latitude,
          longitude: longitude,
        })
        wx.request({
          url: 'https://www.cwlyz.com/wx/Index/address', //仅为示例，并非真实的接口地址
          data: {
            lat: latitude,
            lng: longitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function(res) {
            that.setData({
              addressinfo: res.data.data.address,
              province: res.data.data.province,
              city: res.data.data.city,
              district: res.data.data.district,
            })

          }
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  who: function(e) {
    var who = e.currentTarget.dataset.who;
    this.setData({
      who: who
    })
  },
  age: function(e) {
    var age = e.currentTarget.dataset.age;
    this.setData({
      age: age
    })
  },
  wstatus: function(e) {
    var wstatus = e.currentTarget.dataset.wstatus;
    this.setData({
      wstatus: wstatus
    })
  },
  experience: function(e) {
    var experience = e.currentTarget.dataset.experience;
    this.setData({
      experience: experience
    })
  },
  marriage: function(e) {
    var marriage = e.currentTarget.dataset.marriage;
    this.setData({
      marriage: marriage
    })
  },
  live: function(e) {
    var live = e.currentTarget.dataset.live;
    this.setData({
      live: live
    })
  },
  master: function(e) {
    var master = e.currentTarget.dataset.master;
    this.setData({
      master: master
    })
  },
  money: function(e) {
    var money = e.currentTarget.dataset.money;
    this.setData({
      money: money
    })
  },
  add: function(e) {
    var add = e.currentTarget.dataset.add;
    this.setData({
      add: add
    })
  },
  attitude: function(e) {
    var attitude = e.currentTarget.dataset.attitude;
    this.setData({
      attitude: attitude
    })
  },
  positiontextarea: function(e) {
    var positiontextarea = e.detail.value;
    this.setData({
      positiontextarea: positiontextarea
    })
  },
  addressinfotab: function() {
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        console.log('addressinfotab');
        console.log(res);
        that.setData({
          addressinfo: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        });
        //选择城市区域的时候 需要覆盖之前的 省市区 province city district
        wx.request({
          url: 'https://www.cwlyz.com/wx/Index/address', //仅为示例，并非真实的接口地址
          data: {
            lat: res.latitude,
            lng: res.longitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function(res) {
            that.setData({
              province: res.data.data.province,
              city: res.data.data.city,
              district: res.data.data.district,
            })

          }
        })
      }
    })
  },
  wxhao: function(e) {
    var wxhao = e.detail.value;
    this.setData({
      wxhao: wxhao
    })
  },
  phone: function(e) {
    var phone = e.detail.value;
    this.setData({
      phone: phone
    })
  },
  discripttextarea: function(e) {
    var discript = e.detail.value;
    this.setData({
      discript: discript
    })
  },
  submit: function() {
    wx.showLoading({
      title: '数据提交中...',
    })
    var uid=wx.getStorageSync('userinfo').id;
    var province = this.data.province;
    var city = this.data.city;
    var district = this.data.district;
    var lat = this.data.latitude;
    var lng = this.data.longitude;
    var addressinfo = this.data.addressinfo;
    var who = this.data.who;
    var age = this.data.age;
    var wstatus = this.data.wstatus;
    var experience = this.data.experience;
    var marriage = this.data.marriage;
    var live = this.data.live;
    var master = this.data.master;
    var money = this.data.money;
    var add = this.data.add;
    var attitude = this.data.attitude;
    var wxhao = this.data.wxhao;
    var phone = this.data.phone;
    var discript = this.data.discript;
    var positiontextarea = this.data.positiontextarea;
    var pid=this.data.pid;
   c.ajax(
     cf.hostapi +'Apply/add',
     { who: who, age: age, workstatus: wstatus, experience: experience, marriage: marriage, live: live, master: master, money: money, add: add, attitude: attitude, wx: wxhao, phone: phone, discript: discript, position: positiontextarea, pro: province, city: city, lat: lat, lng: lng, uid: uid, pid: pid, area: district, address: addressinfo},
     res=>{
       if(res.data.code==100){
         wx.hideLoading();
         c.pan('申请成功!');
         setTimeout(function(){
           wx.navigateBack({
             delta: 1
           })
         },1000)
       };

     }
   )

  }
})